const Service = require('../../../core/service/ApiService');

class UserDao extends Service {

    //首页轮播
    async getCarouselimg() {
        return this.model.Picture.findAll({
            where:{
                status:this.constant.PICTURE_STATUS.ENABLE,
            },
            attributes: ['pic_id','picture','ctime'],
            raw: true
        })
    }

    //获取我的设备
    async getDevice(id) {
        const sql = `SELECT user_record.device_id,record_id,device.device_pic as device_pic,device.device_name as device_name,device.position as position,device_number,belong,user_record.ctime as time,user_record.status
                    from user_record
                    inner join device on device.device_id = user_record.device_id
                    where user_record.user_id = '${id}'
                    and user_record.status = ${this.constant.EMPLOY_STATUS.SIGNALED}`;
        return this.model.query(sql, {type: this.model.QueryTypes.SELECT});
    }

    //获取个人信息
    async getInfo(open_id) {
        return this.model.User.findOne({
            where: {
                open_id: open_id
            },
            attributes: ['user_id','wx_id','wx_name','wx_pic','type', 'credit_score'],
            raw: true
        })
    }



    //获取信息
    async getInfoFromDB(user_number,type) {
        return this.model.User.findOne({
            where: {
                user_number:user_number,
                type:type
            },
            attributes: ['user_id']
        })
    }

    //绑定
    async bind(info) {
        return this.model.User.update(info,{
            where: {
                user_number: info.user_number,
                phone:info.phone,
                type:info.type
            }
        });
    }

    //搜索
    async search(belong_number,name) {
        const sql = `select device_id,device_number,device_name,device_pic,position,belong_number,belong,type,status,ctime,mtime
                    from device
                    where (device_name like '%${name}%' 
                    or type like '%${name}%')
                    and belong_number = ${belong_number}
                    order by type`;
        return this.model.query(sql, {type: this.model.QueryTypes.SELECT});
    }

    async getall(belong_number) {
        const sql = `select device_id,device_number,device_name,device_pic,position,belong_number,belong,type,status,ctime,mtime
                    from device
                    where belong_number = ${belong_number}
                    order by type`;
        return this.model.query(sql, {type: this.model.QueryTypes.SELECT});
    }

    //用户更改个人信息
    async changeInfo(info) {
        return this.model.User.update(info,{
            where: {
                open_id: info.open_id
            }
        })
    }

}

module.exports = UserDao;
